﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
namespace RunSopcast
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboChannel.SelectedIndex = 0;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text =  link();
            }
            catch
            {
                MessageBox.Show("Link sai định dạng !");
            }    
        }       
        private string link()
        {
            vn.gametv.GameWS s = new RunSopcast.vn.gametv.GameWS();
            int id = cboChannel.SelectedIndex;
            switch (id)
            {
                case 0: //gametv - 1
                    return s.GetChannelGameTV(1);                    
                case 1: //gametv - 2 
                    return s.GetChannelGameTV(2);
                case 2: //gametv - 3 
                    return s.GetChannelGameTV(3);
                case 3: //gametv - 4 
                    return s.GetChannelGameTV(4);
                case 4: //gametv - 5 
                    return s.GetChannelGameTV(5);
                
                case 5:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=2");
                case 6:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=3");
                case 7:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=4");
                case 8:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=5");
                case 9:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=7");
                case 10:
                    return GetSiteContents("http://giaitriviet.net.vn/vn/tivi.php?id=8");
                    
            }
            return string.Empty;
        }        
        public string GetSiteContents(string url)
        {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;
            do
            {
                count = resStream.Read(buf, 0, buf.Length);
                if (count != 0)
                {
                    tempString = Encoding.UTF8.GetString(buf, 0, count);
                    sb.Append(tempString);
                }
            }
            while (count > 0);
            string path=@"C:\channel.html";
            FileStream f=File.Open(path,FileMode.OpenOrCreate);
            f.Close();
            File.WriteAllText(path,sb.ToString());
            System.Diagnostics.Process.Start("firefox.exe",path);
            return string.Empty;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("sopcast.exe");
            }
            catch
            {
                MessageBox.Show("Lỗi không mở được sopcast !");
            }

        }
    }
}
